package com.burger.counter.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.Session;

import com.burger.counter.beans.Region;
import com.burger.counter.beans.Starter;
import com.burger.counter.beans.User;
import com.burger.counter.business.RegionBusiness;
import com.burger.counter.business.StarterBusiness;
import com.burger.counter.business.UserBusiness;

/**
 * Servlet implementation class UserDataInsert
 */
public class UserDataInsert extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserDataInsert() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			doPost(request, response);
		}
		catch(Exception e)
		{
			System.err.println(e);
		}		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			UserBusiness userBI = new UserBusiness();
			String firstName ="";
			String lastName ="";
			String email ="";
			Integer zip = -1;
			String dateOfBirth="";
			Integer regionData =-1;
			String password ="";
			String isAdminOrCustomer ="C";
			Boolean valid = true;
			HttpSession session = request.getSession(true);
				
			if(request.getParameter("firstName") == null || 
					request.getParameter("firstName") == "")
			{
				valid = false;
			}
	
			if(request.getParameter("lastName") == null || 
					request.getParameter("lastName") == "")
			{
				valid = false;
			}
			if(request.getParameter("email") == null || 
					request.getParameter("email") == "")
			{
				valid = false;
			}
			if(request.getParameter("zip") == null || 
					request.getParameter("zip") == "")
			{
				valid = false;
			}
			
			if(request.getParameter("dateOfBirth") == null || 
					request.getParameter("dateOfBirth") == "")
			{
				valid = false;
			}
	
			if(request.getParameter("regionData") == null || 
					request.getParameter("regionData") == "")
			{
				valid = false;
			}
			if(request.getParameter("password1") == null || 
					request.getParameter("password1") == "")
			{
				valid = false;
			}
			
				
			if(valid)
			{
				
				firstName = request.getParameter("firstName").toString();
				lastName = request.getParameter("lastName").toString();
				email = request.getParameter("email").toString();
				zip = Integer.parseInt(request.getParameter("zip"));
				dateOfBirth = request.getParameter("dateOfBirth").toString();
				regionData = Integer.parseInt(request.getParameter("regionData"));
				password = request.getParameter("password1").toString();
				isAdminOrCustomer = "C";
				
				//Check if the User Already Exists
				User[] userData = userBI.getUserDetails(email);
				
				if(userData != null)
				{
					if(userData.length == 0)
					{
							//No User Data Exists. Insert the data to DB.
							userBI.insertUserDetails
							(firstName, 
								lastName, 
								email, 
								zip, 
								dateOfBirth, 
								regionData, 
								password, 
								isAdminOrCustomer);
							
							//Get the User Id for the User
							User[] userDetails = userBI.getUserDetails(email);
										
							//Store User Id, RegionId and Email Id in session
							session.setAttribute("RegionId", regionData);
							session.setAttribute("UserId", userDetails[0].getUserId());
							session.setAttribute("UserEmailId", email);
							RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/main.jsp");
							dispatcher.forward(request, response);
					}
						else
						{
							session.setAttribute("EmailIdExists", "Email Id already Exists.");
							
							//Get the User Id for the User
							User[] userDetails = userBI.getUserDetails(email);
										
							RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/Registration.jsp");
							dispatcher.forward(request, response);
						}			
			
				}
			}
			else
			{
				System.out.println("Data not valid for insert operation.");
			}
		}
		catch(Exception e)
		{
			System.err.println(e);
		}

	}

}
